import MyRequest from './service'

import type { RequestConfig } from './types'
import { interceptorsHooks } from './interceptorsHooks'

import { appConfig } from '@/config'

/*
 *  该请求只针对 java 请求，如果是 php 或者 其他后端语言， 自行重构
 */

// 二次继承
class RequestService extends MyRequest {
  constructor(config: RequestConfig) {
    super(config)
  }

  request<T = any>(config: RequestConfig, extraConfig?: any): Promise<T> {
    return super
      .request<T>(config, extraConfig)
      .then((response) => response)
      .catch((err) => err)
  }
}

// 取消请求
export const cancelRequest = (url: string | string[]) => {
  return MyRequest.cancelRequest(url)
}
// 取消全部请求
export const cancelAllRequest = () => {
  return MyRequest.cancelAllRequest()
}

// 正常请求
export const service = new RequestService({
  baseURL: appConfig.baseApiUrl,
  interceptors: interceptorsHooks
})

/* 系统设置模块请求 */
export const systemService = new RequestService({
  baseURL: appConfig.systemApiUrl,
  interceptors: interceptorsHooks
})

export default service
export { MyRequest }
